﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AutoLoL.Controls;
using AutoLoL.Entities;
using AutoLoL.Logic;
using AutoLoL.Logic.Factories;
using AutoLoL.Shared;

namespace AutoLoL.Tabs
{
    /// <summary>
    /// Interaction logic for BuildsTab.xaml
    /// </summary>
    public partial class BuildsTab : UserControl
    {
        public BuildsTab()
        {
            InitializeComponent();
            CreateBuildsControl();
        }

        private void SetContent(UIElement content)
        {
            ContainerGrid.Children.Clear();
            ContainerGrid.Children.Add(content);
        }

        public void CreateBuildsControl()
        {
            List<ChampionBuild> championBuilds = ChampionBuildRepository.GetAll(ChampionFactory.Instance, ItemFactory.Instance, SummonerSpellFactory.Instance, MasteryFactory.Instance);
            BuildsControl buildsControl = new BuildsControl(championBuilds);
            buildsControl.Margin = new Thickness(10);
            buildsControl.RequestCreateBuild += new RequestCreateBuild(BuildsControl_RequestCreateBuild);
            buildsControl.RequestEditBuild += new RequestEditBuild(BuildsControl_RequestEditBuild);
            buildsControl.RequestDeleteBuild += new RequestDeleteBuilds(BuildsControl_RequestDeleteBuild);
            SetContent(buildsControl);
        }

        private void CreateEditBuildControl(ChampionBuild championBuild)
        {
            EditBuildControl editBuildControl = new EditBuildControl(championBuild);
            editBuildControl.Margin = new Thickness(10);
            editBuildControl.RequestSaveBuild += new RequestSaveBuild(EditBuildControl_RequestSaveBuild);
            editBuildControl.RequestCancelBuild += new RequestCancelBuild(EditBuildControl_RequestCancelBuild);
            editBuildControl.RequestDeleteBuild += new RequestDeleteBuild(EditBuildControl_RequestDeleteBuild);
            SetContent(editBuildControl);
        }

        private void BuildsControl_RequestCreateBuild(object sender, RequestCreateBuildArgs e)
        {
            Champion champion = e.Champion ?? ChampionFactory.Instance.GetAll()[0];
            ChampionBuild championBuild = ChampionBuildRepository.Create(champion, MasteryFactory.Instance);

            if (championBuild != null)
            {
                CreateEditBuildControl(championBuild);
            }
        }

        private void BuildsControl_RequestEditBuild(object sender, RequestEditBuildArgs e)
        {
            CreateEditBuildControl(e.ChampionBuild);
        }

        private void BuildsControl_RequestDeleteBuild(object sender, RequestDeleteBuildsArgs e)
        {
            ChampionBuildRepository.Delete(e.ChampionBuild);
            CreateBuildsControl();
        }

        private void EditBuildControl_RequestCancelBuild(object sender, RequestCancelBuildArgs e)
        {
            CreateBuildsControl();
        }

        private void EditBuildControl_RequestSaveBuild(object sender, RequestSaveBuildArgs e)
        {
            ChampionBuildRepository.Save(e.ChampionBuild);
            CreateBuildsControl();
        }

        void EditBuildControl_RequestDeleteBuild(object sender, RequestDeleteBuildArgs e)
        {
            ChampionBuildRepository.Delete(e.ChampionBuild);
            CreateBuildsControl();
        }
    }
}
